package com.cndw.kungfu.model.battle.buff;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.cndw.kungfu.domain.BuffMode;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.battle.Battle;
import com.cndw.kungfu.model.battle.Fighter;

/**
 * buff 反射接口,触发buf任务
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-5-26 下午05:10:02
 */
public class BuffInvoke {

	
	
	public static BuffLibI invoke(int buffId, Fighter source, Fighter target, Battle battle) {
		BuffMode buffMode = Context.getSkillServer().getBuffMode(buffId);
		if (buffMode == null) {
			return null;
		}
		Class<?>[] parameterTypes = new Class[] { BuffMode.class, Fighter.class, Fighter.class, Battle.class };
		Object[] params = new Object[parameterTypes.length];
		params[0] = buffMode;
		params[1] = source;
		if (buffMode.isTypeGain()) {
			params[2] = source;
		} else {
			params[2] = target;
		}
		params[3] = battle;

		Object idName = buffMode.getLibId();

		String buffClassName = "com.cndw.kungfu.model.battle.buff.Buff" + idName.toString();

		try {
			Class<?> clazz = Class.forName(buffClassName);
			Object instance = clazz.newInstance();
			Method mGetInstance = clazz.getMethod("initBuffMode", parameterTypes);

			Object obj =  mGetInstance.invoke(instance, params);
			
			if (obj instanceof BuffLibI) {
				return (BuffLibI) obj;
			}

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;

	}

}
